/*
 * @lc app=leetcode.cn id=1035 lang=javascript
 *
 * [1035] 不相交的线
 */

// @lc code=start
/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number}
 */
var maxUncrossedLines = function (nums1, nums2) {
  const m = nums1.length;
  const n = nums2.length;
  const lengths = Array.from({ length: m + 1 }, () => new Array(n + 1).fill(0));
  for (let i = 1; i <= m; i++) {
    for (let j = 1; j <= n; j++) {
      if (nums1[i - 1] === nums2[j - 1])
        lengths[i][j] = lengths[i - 1][j - 1] + 1;
      else {
        lengths[i][j] = Math.max(lengths[i][j - 1], lengths[i - 1][j]);
      }
    }
  }
  return lengths[m][n];
};
// @lc code=end
